<?php
//require_once APPPATH.'controllers/thanhvien/thanhvien.php';
include_once (dirname(__FILE__) . "/thanhvien.php");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of thanhvien_diachi
 *
 * @author Bui
 */
class Thanhvien_diachi extends CI_Controller {
    
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('thanhvien_diachi_model');
        $this->load->library('GMap');
        $this->load->library('form_validation');
    }

    function load_diachi($id) {
        $query = $this->thanhvien_diachi_model->get_diachi($id);
        return $query;
        $data['view'] = 'user/thanhvien/home_diachi_view';
        //$this->load->view('user/thanhvien/home_diachi_view', $data);
        
    }

    function render($path, $data) {
        $this->load->view('user/common/header_view', $data);
        $this->load->view('user/common/left_sidebar_view', $data);
        $this->load->view('user/common/head_content_view', $data);
        $this->load->view($path, $data);
        $this->load->view('user/common/right_sidebar_view', $data);
        $this->load->view('user/common/footer_view', $data);

        
    }

    function update_diachi($id) {
              
        $query = $this->thanhvien_diachi_model->id_exists($id);
        // form validation
        
        if ($query == TRUE) {
            //da ton tai
            $this->thanhvien_diachi_model->update_diachi($id);
        } else {
            
            $this->thanhvien_diachi_model->add_diachi($id);
        }
        $this->load_map_2($id); 
        //load len lai trang home
        redirect('/thanhvien/thanhvien/home/', 'refresh');
        
    }

    function check_id_exists($id) {
        $query = $this->thanhvien_diachi_model->id_exists($id);
        return $query;
    }

    function load_map_2($id) {  
        $this->gmap->GoogleMapAPI();
        $this->gmap->setMapType('normal');
        $user= $this->thanhvien_diachi_model->get_diachi($id);
        $this->gmap->addMarkerByAddress($user['diachi'] . "," . $user['xaphuong'] . "," . $user['quanhuyen'] . "," . $user['tinhthanh']
                , "<h4>Địa chỉ hiện tại:</h4> " . $user['diachi'] . "," . $user['xaphuong'] . "," . $user['quanhuyen'] . "," . $user['tinhthanh'], "Địa chỉ hiện tại của bạn.");
        $data['headerjs'] = $this->gmap->getHeaderJS();
        $data['headermap'] = $this->gmap->getMapJS();
        $data['onload'] = $this->gmap->getOnLoad();
        $data['map'] = $this->gmap->getMap();
        $data['sidebar'] = $this->gmap->getSidebar();
//        $this->load->view('user/common/header_view', $data);
//        $this->load->view('user/common/head_content_view', $data);
//        $this->load->view('user/thanhvien/show_map', $data);
//        $this->load->view('user/common/tail_content_view', $data);
//        $this->load->view('user/common/footer_view', $data);
   //$this->render('user/thanhvien/show_map', $data);  
        return $data;
        
    }

    function load_map($id) {
        $this->gmap->GoogleMapAPI();
        $this->gmap->setMapType('normal');
        //$user = $this->thanhvien_diachi_model->get_diachi($id);
        $this->gmap->addMarkerByAddress('59 duong so 5, phuong linh chieu, quan thu duc , Ho Chi Minh',"","TRUNG TÂM GIA SƯ 7S");
        $data['headerjs'] = $this->gmap->getHeaderJS();
        $data['headermap'] = $this->gmap->getMapJS();
        $data['onload'] = $this->gmap->getOnLoad();
        $data['map'] = $this->gmap->getMap();
        $data['sidebar'] = $this->gmap->getSidebar();
        $data['info'] = '<div class="text"><i>Thông tin đang chờ cập nhật</i></div>';
//        $this->load->view('user/thanhvien/show_map', $data);
        
        return $data;
    }

}

?>
